var express = require("express");
var router = express.Router();

// 引入封装的 mysql模块
const mysql = require("../util/mysql/mysql");
//项目详情
router.post("/projectData", function (req, res, next) {
  const p_id = req.body.p_id;
  mysql.data(
    "select * from project a INNER JOIN project_mainmas b INNER JOIN teacher_mes c INNER JOIN student_mes d  ON a.id = b.p_id AND a.p_teacher_id=c.t_id AND a.p_apply_id=d.s_id WHERE p_id=?",
    [p_id],
    function (err, rows, fields) {
      if (err) {
        res.send("错误");
        return;
      } else {
        res.send(JSON.stringify({ code: 1, projectData: rows[0] }));
      }
    }
  );
});
//待审批
router.post("/checkPending", function (req, res, next) {
  const e_id = req.body.e_id;
  mysql.data(
    "select * from project a INNER JOIN project_mainmas b INNER JOIN teacher_mes c INNER JOIN student_mes d  ON a.id = b.p_id AND a.p_teacher_id=c.t_id AND a.p_apply_id=d.s_id WHERE p_expert_id=? AND p_type=11",
    [e_id],
    function (err, rows, fields) {
      if (err) {
        res.send("错误");
        return;
      } else {
        res.send(JSON.stringify({ code: 1, dataList: rows }));
      }
    }
  );
});
//已审批
router.post("/overPending", function (req, res, next) {
  const e_id = req.body.e_id;
  mysql.data(
    "select * from project a INNER JOIN project_mainmas b INNER JOIN teacher_mes c INNER JOIN student_mes d  ON a.id = b.p_id AND a.p_teacher_id=c.t_id AND a.p_apply_id=d.s_id WHERE p_expert_id=? AND p_type=1 OR p_type=2",
    [e_id],
    function (err, rows, fields) {
      if (err) {
        res.send("错误");
        return;
      } else {
        res.send(JSON.stringify({ code: 1, dataList: rows }));
      }
    }
  );
});
//审批留言
router.post("/applyMes", function (req, res, next) {
  const applyMes = [
    req.body.al_expert,
    req.body.al_project,
    req.body.al_teacher,
    req.body.al_student,
    req.body.al_main,
    req.body.al_type,
    req.body.al_time,
  ];
  mysql.data(
    "INSERT INTO apply_mes (al_expert,al_project,al_teacher,al_student,al_main,al_type,al_time) VALUES (?,?,?,?,?,?,?)",
    applyMes,
    function (err, rows1, fields) {
      if (err) return;
      if(req.body.al_type ==1){
        mysql.data(
            "UPDATE project SET p_type=1 WHERE id=?",
            [req.body.al_project],
            function (err, row2s, fields) {
              if (err) return;
              res.send(JSON.stringify({ code: 1 }));
            }
          )
      }else{
        mysql.data(
            "UPDATE project SET p_type=10 WHERE id=?",
            [req.body.al_project],
            function (err, row2s, fields) {
              if (err) return;
              res.send(JSON.stringify({ code: 1 }));
            }
          )
      }
      
    }
  );
});
//我的信息
router.post("/MyMes", function (req, res, next) {
    const e_id = req.body.e_id;
    //个人信息
    const p1 = new Promise((res1, rej2) => {
      mysql.data(
        "SELECT * FROM expert_mes WHERE e_id=?",
        [e_id],
        function (err, rows, fields) {
          if (err) return;
          res1(rows[0]);
        }
      );
    });
    // 我的项目
    const p3 = new Promise((res3, rej3) => {
      mysql.data(
          "SELECT id,p_title,p_synopsis,p_type FROM project a INNER JOIN project_mainmas b ON a.id=b.p_id WHERE p_expert_id=?",
          [e_id],
          function (err, rows, fields) {
              if (err) return; 
            res3(rows);
          }
        );
    });
    Promise.all([p1, p3]).then((resall) => {
      res.send(
        JSON.stringify({
          code: 1,
          expertMes: {
            mes: resall[0],
            project: resall[1],
          },
        })
      );
    });
  });
 
  
module.exports = router;
